//
//  AppProTextFieldBaseView.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/3/17.
//

import UIKit

class AppProTextFieldBaseView: UIView, UITextFieldDelegate {

    lazy var icon = UIImageView()
    lazy var titleLb = UILabel()
    lazy var lineView = UIView()
    lazy var textField = UITextField()
    lazy var button = UIButton()
    
    var maxCharacterCount: Int {
        24
    }
    var kAlphaNum: String {
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    }
    
    var textContent: String {
        textField.text ?? ""
    }
    
    var tfChangedCallback: ((String) -> Void)?
    var tfEndCallback: ((String) -> Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubViews()
        relayoutSubViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func addSubViews() {
        addSubview(lineView)
        addSubview(icon)
        addSubview(titleLb)
        addSubview(textField)
        addSubview(button)
        
//        titleLb.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        titleLb.setContentHuggingPriority(.required, for: .horizontal)
        
        let cFont = AppFont.harmonyOSSans(ofSize: 19, weight: .medium)
        let cColor = UIColor(hex: 0x000000, transparency: 0.9)
        titleLb.font = cFont
        titleLb.textColor = cColor
//        titleLb.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        
        lineView.isHidden = true
        lineView.backgroundColor = UIColor(hex: 0x000000)
        lineView.alpha = 0.08
//        lineView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        
//        textField.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        textField.setContentHuggingPriority(.defaultLow, for: .horizontal)
        
        textField.textColor = cColor
        textField.font = cFont
//        textField.tintColor = UIColor(hex: 0x2DC84D)
        textField.tintColor = UIColor(hex: 0x007AFF)!
        textField.keyboardAppearance = .light
        textField.delegate = self
        textField.addTarget(self, action: #selector(textFieldChanged(sender:)), for: .editingChanged)
        textField.addTarget(self, action: #selector(textFieldEnd(sender:)), for: .editingDidEnd)
        
        button.addTarget(self, action: #selector(clickButton(sender:)), for: .touchUpInside)
    }
    
    func relayoutSubViews() {
        
    }
    
    @objc func clickButton(sender: UIButton) {
        
    }
    
    // MARK: - UITextField
    @objc func textFieldChanged(sender: UITextField) {
        tfChangedCallback?(textContent)
    }
    @objc func textFieldEnd(sender: UITextField) {
        tfEndCallback?(textContent)
    }
    
    // MARK: - UITextFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.isEmpty {
            return true
        }
        
        var isOk = false
        
        if kAlphaNum.isEmpty {
            isOk = true
        } else {
            let cs = CharacterSet(charactersIn: kAlphaNum).inverted
            let filtered = string.components(separatedBy: cs).joined(separator: "")
            
            isOk = (string == filtered)
        }
        
        if isOk {
            let textCount = textField.text?.count ?? 0
            if textCount < maxCharacterCount {
                return true
            }
            
            return false
        }
        
        return false
    }

}
